﻿// <copyright file="HandleTimePicker.cs" company="Microsoft Corporation">
// Copyright (c) 2010 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-11-01</date>
// <summary>TimePicker TrackAction Handler</summary>

namespace Microsoft.WebAnalytics.Toolkit
{
    using System;
    using System.ComponentModel.Composition;
    using Microsoft.Phone.Controls;
    using Microsoft.WebAnalytics;
    using Microsoft.WebAnalytics.Contracts;

    /// <summary>
    /// Track a Windows Phone 7 TimePicker ValueChanged event
    /// </summary>
    /// <remarks>
    /// <list type="table">
    /// <item>
    /// <term>Platforms</term>
    /// <description>Windows Phone 7</description>
    /// </item>
    /// <item>
    /// <term>AnalyticsEvent.Name</term>
    /// <description>ValueChanged</description>
    /// </item>
    /// <item>
    /// <term>AnalyticsEvent.ActionValue</term>
    /// <description>the new time</description>
    /// </item>
    /// </list>
    /// </remarks>
    [Tracker(typeof(TimePicker))]
    public class HandleTimePicker : ITrackHandler
    {
        #region Properties
        /// <summary>
        /// Gets or sets the event Logging method
        /// </summary>
        [Import("Log")]
        public Action<AnalyticsEvent> Log { get; set; }
        #endregion

        #region ITrackAction Members

        /// <summary>
        /// Track the TimePicker ValueChanged event
        /// </summary>
        /// <param name="associatedObject">the associated object</param>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="eventArgs">the event arguments</param>
        public void Track(
            System.Windows.FrameworkElement associatedObject,
            AnalyticsEvent analyticsEvent,
            System.EventArgs eventArgs)
        {
            if (analyticsEvent.Name == "ValueChanged")
            {
                var args = eventArgs as DateTimeValueChangedEventArgs;

                if (args.NewDateTime.HasValue)
                {
                    analyticsEvent.ActionValue = args.NewDateTime.Value.ToString("T");
                }
            }

            this.Log(analyticsEvent);
        }

        #endregion
    }
}
